package com.java.controller;

import com.java.dao.MaintainRecordsDao;
import com.java.dao.SparePartsDao;
import com.java.dao.SparePartsFlowDao;
import com.java.entity.MaintainRecords;
import com.java.entity.SpareParts;
import com.java.entity.SparePartsFlow;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 维修记录(MaintainRecords)表控制层
 *
 * @author 安能
 * @since 2020-07-01 11:27:28
 */
@RestController
@RequestMapping("MaintainRecords")
public class MaintainRecordsController {

    @Resource
    private MaintainRecordsDao maintainRecordsDao;


    @Resource
    private SparePartsFlowDao sparePartsFlowDao;

    @Resource
    private SparePartsDao sparePartsDao;



    @GetMapping("insert")
    public MaintainRecords insert(MaintainRecords maintainRecords) {

        Integer sparePartsId = maintainRecords.getSparePartsId();
        if (sparePartsId != null) {
            Integer count = maintainRecords.getCount();
            SpareParts spareParts = sparePartsDao.selectById(sparePartsId);
            spareParts.setCount(spareParts.getCount()-count);
            SparePartsFlow sparePartsFlow = new SparePartsFlow();
            sparePartsFlow.setSparePartsId(sparePartsId);
            sparePartsFlow.setSparePartsName(spareParts.getSparePartsName());
            sparePartsFlow.setType(1);
            sparePartsFlow.setCount(count);
//            spareParts.setId(sparePartsId);
//            spareParts.setCount(sparePartsId);
            sparePartsDao.updateById(spareParts);
            sparePartsFlowDao.insert(sparePartsFlow);
        }


        maintainRecordsDao.insert(maintainRecords);
        return maintainRecords;
    }

    @GetMapping("selectList")
    public HashMap<String, Object> selectList(Integer id) {

        List<MaintainRecords> maintainRecords = maintainRecordsDao.selectList(null);

        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", maintainRecords.size());
        map.put("data", maintainRecords);
        return map;
    }
}